package Client;


import Message.AccountSignUpInfo;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

//TODO  注册功能仍然是直接连接了数据库，应该创建一个服务端处理注册业务，并且需要修改客户端。
public class SignUpActionListener implements ActionListener {

    User user;
    private SignUpUI signUpUI;
    private boolean isChecked;
    private AccountSignUpInfo accountSignUpInfo;

    SignUpActionListener(SignUpUI s) {
        this.signUpUI = s;
    }

    //密码合法性的检查：8-20位的数字或字符
    public static boolean isPassword(String password) {
        return password.length() >= 8 && password.length() <= 20;
    }

    //uid的合法性检查
    //注册时生成的id必定为9位的数字，所以检查有效性的时候也要满足。
    public static boolean isUserId(String userId) {
        try {
            int i1 = Integer.parseInt(userId);
            i1 /= 100000000;
            return i1 > 0 && i1 < 10;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == signUpUI.bCheckUid) {

        }
        if (e.getSource() == signUpUI.bSignUp) {
            String uid, upass;
            uid = SignUpUI.tUid.getText();
            upass = String.valueOf(SignUpUI.tPassword.getPassword());

            if (uid.equals("")) {
                JOptionPane.showMessageDialog(null, "请输入注册账号！", "消息提示", JOptionPane.WARNING_MESSAGE);
                return;
            }

            if (upass.equals("")) {
                JOptionPane.showMessageDialog(null, "请输入注册密码！", "消息提示", JOptionPane.WARNING_MESSAGE);
                return;
            }

            if (!isPassword(upass)) {
                JOptionPane.showMessageDialog(null, "密码应为8-20位的数字或字符组成！", "消息提示", JOptionPane.WARNING_MESSAGE);
                return;
            }

            if (!isUserId(uid)) {
                JOptionPane.showMessageDialog(null, "账号应为9位的数字组成！", "消息提示", JOptionPane.WARNING_MESSAGE);
                return;
            }
            accountSignUpInfo = new AccountSignUpInfo(uid, upass);
            try {
                //将accountSignUpinfo传给服务端。
                Socket socket = new Socket(ServerInfo.SERVER_IP_ADDRESS, ServerInfo.SERVER_SIGNUP_PORT);
                OutputStream outputStream = socket.getOutputStream();
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
                objectOutputStream.writeObject(accountSignUpInfo);
                //获得服务器的回应。
                InputStream inputStream = socket.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String res = null;
                res = bufferedReader.readLine();

                if (res.equals("true")) {
                    JOptionPane.showMessageDialog(null, "注册成功，请记住账号密码！", "消息提示", JOptionPane.WARNING_MESSAGE);
                    return;
                } else {
                    JOptionPane.showMessageDialog(null, "注册失败，账号已经被注册！", "消息提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }

            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "注册服务器不在线！", "消息提示", JOptionPane.WARNING_MESSAGE);
                System.exit(0);
            }


        } else if (e.getSource() == signUpUI.bCancel) {
            signUpUI.dispose();
        }
    }

}
